$(document).ready(function() {	
//	findAllDeptType();
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventLevel.action",
  		success:function(data){
  			$("<option value=''>请选择</option>").appendTo("#eventLevel");
  			$.each(data.eventLevels,function(i,n){
  				$("<option value="+n.elId+">"+n.elEventLevel+"</option>").appendTo("#eventLevel");
			});
  		}
	});
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventType.action",
  		success:function(data){
  			$("<option value=''>请选择</option>").appendTo("#eventType");
  			$.each(data.eventTypes,function(i,n){
  				$("<option value="+n.etId+">"+n.etEventType+"</option>").appendTo("#eventType");
			});
  		}
	});
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventSource.action",
  		success:function(data){
  			$.each(data.eventSources,function(i,n){
  				$("<option value="+n.esId+">"+n.esEventSource+"</option>").appendTo("#eventSource");
			});
  		}
	});
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventStatus.action",
  		success:function(data){
  			$.each(data.eventStatuses,function(i,n){
  				$("<option value="+n.tbesId+">"+n.tbesEventStatus+"</option>").appendTo("#eventStatus");
			});
  		}
	});	
	function submitToLeader(data){
		$("#saving").remove();
		if(data.ajaxRst!="true"){
			alert("添加事件失败！")
			return;
		}
		$("#notifyLeaderWindow").dialog("open");
		$("#eventId").append(data.event.EId);
		if(data.leader!=null){
			$("#leader").append(data.leader.UName).css("color","#960");
			$("#leaderId").append(data.leader.UId);
		}else{
			$("#leader").append("没找到今天的带班领导").css("color","red").css("font-weight","bold");
		}
		$("#smsContent").append("现有\""+data.event.EEventname+"\"事件发生，受理单号是"+data.event.EId+"。请您尽快登录系统处理。");
/*		if(data.ajaxRst){
			var strHTML="<div id='smsp' class='faqbg'></div>";
			strHTML+="<div id='subsmsp' class='faqdiv' style='display:none'>";
			strHTML+="<h2>事件保存成功<span class='close'>关闭</span></h2>";
			strHTML+="<div class='subArea'>受理单号：<span style='color:red;font-size:18'>"+data.event.EId+"</span></div>";
			strHtml+="<div class='subArea'><button class='selUser'>选择要通知的人员</button></div>";
			if(data.leader!=null){
				strHTML+="<div class='subArea leader'>带班领导："+data.leader.UName+"</div>";
			}else{
				strHTML+="<div class='subArea leader'></div>";
			}
			strHTML+="<div class='subArea'><span style='float: left;'>通知内容：</span>";
			strHTML+="<textarea id='smsContent' style='width:320;height:120'>现有\""+data.event.EEventname+"\"事件发生，受理单号是"+data.event.EId+"。请您尽快登录系统处理。</textarea></div>";
			strHTML+="<div ><span style='padding-left:290'><button id='submit' class='submit'>通知</button></span>";
			strHTML+=" <span style='padding-left:10'><button class='submit close'>取 消</button></span></div><br />";
			strHTML+="</div>"
			//$("body").append(strHTML);
			$("#smsp").css({display:"block",height:$(document).height()});
			$("#subsmsp").css("display","block");
			document.documentElement.scrollTop=0;
			$(".close").click(function(){
				$("#smsp").remove();
	  			$("#subsmsp").remove();
			});
			$("#submit").click(function(){
				var phone = $("#phone").text();
				var sms = $("#smsContent").text();
				confirm("短信发送成功！");
				window.location.href="/ag/wzp/event_man.jsp";
			});
			$(".selUser").click(function(){
				alert("d")
				$("#deptChooseWindow").dialog("open");
				$(".leader").empty();
				$(".leader").append("qqqqqq");
				alert("t")
			});
		}	
		*/			
	}
	$('.num').numeric();
	$('.dou').numeric({allow:"."});
	initvalidator();
	$("#acceptForm").validate({
		submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form //???? 
       		//var content = "<img src='wzp/images/loading.gif'/>" +
       		//"<span style='padding-left:15;padding-bottom:20;'>正在保存...<span> ";
			//TINY.box.show(content,0,0,0,3);
			//form.submit();   //提交表单
			var strHTML="<div id='saving'><div id='s' class='faqbg'></div>";
				strHTML+="<div id='su' class='loadingdiv' >";
				strHTML+="<img src='wzp/images/loading.gif' />";
				strHTML+="  正在保存事件....</div></div>"
			$("body").append(strHTML);
			$("#s").css({display:"block",height:$(document).height()});
			document.documentElement.scrollTop=0;
			var event={
				"event.tbEventSource.esId":$('#eventSource').attr('value'),
				"event.ESubmiterContact":$('#submitContact').attr('value'),
				"event.ESubmitTime":$('#submitTime').attr('value'),
				"event.ESubmiter":$('#submiter').attr('value'),
				//"event.ESubmitDept":$('#submitDept').attr('value'),
				"event.tbUserByUId.UId":$('#user').attr('value'),
				"event.EEventAddr":$('#eventAddr').attr('value'),
				"event.ELongitude":$('#longitude').attr('value'),
				"event.ELatitude":$('#latitude').attr('value'),
				"event.EEventtime":$('#eventTime').attr('value'),
				"event.EEventname":$('#eventName').attr('value'),
				"event.tbEventType.etId":$('#eventType').attr('value'),
				"event.EDeaths":$('#death').attr('value'),
				"event.EInjuries":$('#injuries').attr('value'),
				"event.tbEventLevel.elId":'2000',//未定级
				"event.EEventCause":$('#eventCause').attr('value'),
				"event.EEventDesc":$('#eventDesc').attr('value'),
				"event.ETakeMeasure":$('#takeMeasure').attr('value'),
				"event.tbEventStatus.tbesId":'1001',//事件状态：接报
				
				"event.c":$("#tapeRecord").text()//录音，一个URL
			}
			$.ajax({
  				type: "post",
 				url: "wzp/event_save.action",
 				data:event,
  				success:function(data){
  					submitToLeader(data)
  				}
			});	
        },
		rules: {
			'event.ESubmiterContact':{
				required:true,
				digits:true,
				maxlength:18
			},
			'event.ESubmitTime':'required',
			'event.ESubmiter':'required',
			'event.ESubmitDept':'required',
			'event.EEventAddr':{
				required:true,
				maxlength: 50
			},
			'event.ELongitude':{
				reuqierd:false,
				number: true,
				maxlength: 20
			},
			'event.ELatitude':{
				reuqierd:false,
				number: true,
				maxlength: 20
			},
			'event.tbEventType.etId':'required',
			'event.EEventtime':'required',
			'event.EEventname':{
				required:true,
				maxlength: 30
			},
			'event.EDeaths':{
				reuqierd:false,
				digits: true
			},
			'event.EInjuries':{
				reuqierd:false,
				digits: true
			},
			'event.tbUserByUId.UId':'required',
			'event.tbEventLevel.elId':'required',
			'event.EEventCause':'required',
			'event.EEventDesc':'required'
		},
		messages: {
			'event.ESubmiterContact':"请输入正确的号码",
			'event.ESubmitTime':'*',
			'event.ESubmiter':"*",
			'event.ESubmitDept':"*",
			'event.EEventAddr':"*50字之内",
			'event.ELongitude':"最多20位数字" ,
			'event.ELatitude':"最多20位数字",
			'event.tbEventType.etId':"*必须选",
			'event.EEventtime':"*",
			'event.EEventname':"*30字之内",
			'event.EDeaths':"数字",
			'event.EInjuries':"数字",
			'event.tbUserByUId.UId':"请先登录",
			'event.tbEventLevel.elId':"*必须选",
			'event.EEventCause':"",
			'event.EEventDesc':""
		}
	});
	
});

/**
 * 
 * @param tapeUrl 
 * @return
 */
function settapeRecord(tapeUrl){
	$("#tapeRecord").text(tapeUrl);
}

function initvalidator() {
	
	jQuery.validator.addMethod("chinese", function (value, element) {
		//alert("chinese validator");
		var chinese = /^[\u4e00-\u9fa5]+$/;
		return this.optional(element) || chinese.test(value);
	}, "\u8bf7\u8f93\u5165\u4e2d\u6587");
	jQuery.validator.addMethod("isMobile", function (value, element) {
		//alert("Mobile validator");
		var length = value.length;
		var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
		return this.optional(element) || (length == 11 && mobile.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u624b\u673a\u53f7\u7801"); 
	
	// 电话号码验证      
	jQuery.validator.addMethod("isTel", function (value, element) {
		//alert("Tel validator"); 
		var tel = /^\d{3,4}-?\d{7,9}$/;    //电话号码格式010-12345678  
		return this.optional(element) || (tel.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u7535\u8bdd\u53f7\u7801,\u683c\u5f0f010-12345678");
	
	// 联系电话(手机/电话皆可)验证  
	jQuery.validator.addMethod("isPhone", function (value, element) {
		//alert("Phone validator");
		var length = value.length;
		var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
		var tel = /^\d{3,4}-?\d{7,9}$/;
		return this.optional(element) || (tel.test(value) || mobile.test(value));
	}, "\u8bf7\u6b63\u786e\u586b\u5199\u60a8\u7684\u8054\u7cfb\u7535\u8bdd");
}